// base.js大约10000行左右

let Kv = {
  VZ: function(a) {
      a.reverse()
  },
  iG: function(a, b) {
      var c = a[0];
      a[0] = a[b % a.length];
      a[b % a.length] = c
  },
  k8: function(a, b) {
      a.splice(0, b)
  }
}

let Lv = function(a) {
  a = a.split("");
  Kv.VZ(a, 25);
  Kv.k8(a, 1);
  Kv.VZ(a, 75);
  Kv.k8(a, 1);
  Kv.VZ(a, 30);
  Kv.k8(a, 1);
  return a.join("")
}

/*
let isInsert=false;
let aajjaaxx=unsafeWindow.XMLHttpRequest;
unsafeWindow.allAjax=[];
var C=function(){
  let a=new aajjaaxx();
  unsafeWindow.allAjax.unshift(a);//保存所有ajax请求
  return a;
}
unsafeWindow.XMLHttpRequest=C;

let timer=setInterval(()=>{
let tmpArr=unsafeWindow.allAjax;
tmpArr=tmpArr.reverse();
for (let item of tmpArr){
  if (item && item.responseURL && decodeURIComponent(item.responseURL).indexOf('googlevideo.com/videoplayback')!==-1){
    unsafeWindow.pjObj=getQuery(item.responseURL.split('?')[1])
    if (!isInsert){
      insertHtml()
      isInsert=true
    }
    return;
  }
}
},300)*/
insertHtml()

function insertHtml(){
let $=window.jQuery
let str=unsafeWindow.ytplayer.config.args.adaptive_fmts
let tmpArr=str.split(',')
let formats=[]
for (let item of tmpArr){
  let obj=getQuery(item)
  obj.type=decodeURIComponent(obj.type)
  obj.rawUrl=decodeURIComponent(obj.url)
  obj.urlInfo=getQuery(obj.rawUrl.split('?')[1])
  formats.push(obj)
}
let html=''
for (let item of formats){
  let toadd=item.s?`&alr=yes&sig=${Lv(item.s.replace(/%3D/g,'='))}`:''
  
  html+=`<a href="${item.rawUrl}${toadd}" target="_black">${item.size || ''}  ${item.type}</a><br/>`
}

let timer2=setInterval(()=>{
  if ($('div#download-infomation').length<1){
      $("div#clarify-box").append(`
      <div id="download-infomation">
        ${html}
      </div>
    `)
  }
  
},1000)
}


function getQuery(str,key){
  let searchStr=str
  let resObj={}
  if (searchStr){
      let searchArr=searchStr.split('&')
      for (let item of searchArr){
          let itemArr=item.split('=')
          resObj[itemArr[0]]=itemArr[1]
      }

      if (key){
          return resObj[key]
      }else{
          return resObj
      }
  }else{
      return null
  }
}